package com.ruoyi.project.qyxy.orgdtl.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.project.qyxy.persondtl.domain.QuaPersonSocreDetail;
import com.ruoyi.project.qyxy.persondtl.service.IQuaPersonSocreDetailService;
import com.ruoyi.project.qyxy.qua.domain.QuaInfo;
import com.ruoyi.project.qyxy.qua.domain.QuaTemp;
import com.ruoyi.project.qyxy.quadtl.domain.QuaSocreDetail;
import com.ruoyi.project.qyxy.quadtl.service.IQuaSocreDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.qyxy.orgdtl.mapper.OrgScoreDetailMapper;
import com.ruoyi.project.qyxy.orgdtl.domain.OrgScoreDetail;
import com.ruoyi.project.qyxy.orgdtl.service.IOrgScoreDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业得分明细Service业务层处理
 * 
 * @author lcl
 * @date 2021-03-05
 */
@Service
public class OrgScoreDetailServiceImpl implements IOrgScoreDetailService 
{
    @Autowired
    private OrgScoreDetailMapper orgScoreDetailMapper;

    //资质得分详细
    @Autowired
    private IQuaSocreDetailService quaSocreDetailService;

    @Autowired
    private IQuaPersonSocreDetailService quaPersonSocreDetailService;

    /**
     * 查询企业得分明细
     * 
     * @param id 企业得分明细ID
     * @return 企业得分明细
     */
    @Override
    public OrgScoreDetail selectOrgScoreDetailById(Long id)
    {
        return orgScoreDetailMapper.selectOrgScoreDetailById(id);
    }

    /**
     * 查询企业得分明细列表
     * 
     * @param orgScoreDetail 企业得分明细
     * @return 企业得分明细
     */
    @Override
    public List<OrgScoreDetail> selectOrgScoreDetailList(OrgScoreDetail orgScoreDetail)
    {
        return orgScoreDetailMapper.selectOrgScoreDetailList(orgScoreDetail);
    }

    /**
     * 新增企业得分明细
     * 
     * @param orgScoreDetail 企业得分明细
     * @return 结果
     */
    @Transactional(rollbackFor = {RuntimeException.class})
    @Override
    public int insertOrgScoreDetail(OrgScoreDetail orgScoreDetail,QuaInfo qua)
    {
        int i = orgScoreDetailMapper.insertOrgScoreDetail(orgScoreDetail);
        //添加资质记录
        QuaSocreDetail quaSocreDetail = new QuaSocreDetail();
        quaSocreDetail.setDetailId(orgScoreDetail.getDetailId());
        quaSocreDetail.setOrgCode(qua.getOrgCode());
        quaSocreDetail.setQuaType(qua.getQuaType());
        quaSocreDetail.setQuaName(qua.getQuaName());
        quaSocreDetail.setQuaRank(qua.getQuaRank());
        quaSocreDetail.setQuaNum(qua.getQuaNum());
        quaSocreDetail.setIsMainQua(qua.getIsMainQua());
        quaSocreDetail.setApproveBy(qua.getApproveBy());
        quaSocreDetail.setDtApprove(qua.getDtApprove());
        quaSocreDetail.setDtExpire(qua.getDtExpire());
        quaSocreDetail.setQuaCode(qua.getQuaCode());
        quaSocreDetailService.insertQuaSocreDetail(quaSocreDetail);
        return i;
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    @Override
    public int insertOrgScoreDetail(OrgScoreDetail orgScoreDetail, List<QuaTemp> quaTempList) {
        int i = orgScoreDetailMapper.insertOrgScoreDetail(orgScoreDetail);
        for (QuaTemp quaTemp : quaTempList) {
            QuaPersonSocreDetail detail = new QuaPersonSocreDetail();
            detail.setDetailId(orgScoreDetail.getId());
            detail.setQuaName(quaTemp.getQuaInfo().getQuaName());
            detail.setQuaType(quaTemp.getQuaInfo().getQuaType());
            detail.setQuaRunk(quaTemp.getQuaInfo().getQuaRank());
            detail.setIsConformTo(quaTemp.getQualified() ? "是" : "否");
            detail.setAddScore(quaTemp.getAddScore());
            detail.setReduceScore(quaTemp.getReduceScoer());
            detail.setUseZzPerson(JSONObject.toJSONString(quaTemp.getZzPersonList()));
            detail.setUseZcPerson(JSONObject.toJSONString(quaTemp.getZcPersonList()));
            detail.setAddScorePerson(JSONObject.toJSONString(quaTemp.getAugmentZzPersonList()));
            detail.setShortOfZz(quaTemp.getShortOfZzCount().toString());
            detail.setShortOfZc(quaTemp.getShortOfZcCount().toString());
            detail.setShortOfZcType(JSONObject.toJSONString(quaTemp.getShortOfZcTypeList()));
            quaPersonSocreDetailService.insertQuaPersonSocreDetail(detail);

        }
        return i;
    }

    /**
     * 修改企业得分明细
     * 
     * @param orgScoreDetail 企业得分明细
     * @return 结果
     */
    @Override
    public int updateOrgScoreDetail(OrgScoreDetail orgScoreDetail)
    {
        return orgScoreDetailMapper.updateOrgScoreDetail(orgScoreDetail);
    }

    /**
     * 批量删除企业得分明细
     * 
     * @param ids 需要删除的企业得分明细ID
     * @return 结果
     */
    @Override
    public int deleteOrgScoreDetailByIds(Long[] ids)
    {
        return orgScoreDetailMapper.deleteOrgScoreDetailByIds(ids);
    }

    /**
     * 删除企业得分明细信息
     * 
     * @param id 企业得分明细ID
     * @return 结果
     */
    @Override
    public int deleteOrgScoreDetailById(Long id)
    {
        return orgScoreDetailMapper.deleteOrgScoreDetailById(id);
    }

    @Override
    public Double getSumScore(String orgCode) {
        return orgScoreDetailMapper.getSumScore(orgCode);
    }

    @Override
    public OrgScoreDetail getByOrgCode(String orgCode,String type) {
        return orgScoreDetailMapper.getByOrgCode(orgCode,type);
    }
}
